package com.cdream.basic.algorithm.leetcode.dp;

import java.util.Arrays;

/**
 * @author cdream
 * @date 6/8/21
 */
public class Question1049 {
    public int lastStoneWeightII(int[] stones) {
        int sum = Arrays.stream(stones).sum();

        int totalWeight = sum / 2;
        int[] dp = new int[totalWeight + 1];
        for (int i = 1; i <= stones.length; i++) {
            int weight = stones[i-1];
            for (int i1 = totalWeight; i1 >= weight; i1--) {
                dp[i1] = Math.max(dp[i1], dp[i1 - weight] + weight);
            }
        }
        return sum-2*dp[totalWeight];

    }
}
